package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.SystemNotice;
import cn.wolfcode.trip.base.query.SystemNoticeQuery;
import cn.wolfcode.trip.base.service.ISendVerifyCodeService;
import cn.wolfcode.trip.base.service.ISystemNoticeService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 系统通知页面的控制器
 */
@Controller
@RequestMapping("systemNotice")
public class SystemNoticeController {

    @Autowired
    private ISystemNoticeService noticeService;
    @Autowired
    private IUserService userService;
    @Autowired
    ISendVerifyCodeService sendVerifyCodeService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") SystemNoticeQuery qo, Model model) {
        model.addAttribute("pageInfo", userService.query(qo));
        return "systemNotice/list";
    }

    //发送通知的方法
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(SystemNotice entity, Boolean type) {
        JSONResult jsonResult = new JSONResult();
        System.out.println(type);

        try {
            if (type == false) {
                noticeService.saveOrUpdate(entity);
            } else if (type == true) {
                noticeService.saveOrUpdate(entity);
                sendVerifyCodeService.send(entity.getUser().getPhone(), entity.getContent());
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("异常");
        }
        return jsonResult;
    }

    //已发送通知页面的方法
    @RequestMapping("release")
    public String releaseList(@ModelAttribute("qo") SystemNoticeQuery qo, Model model) {
        model.addAttribute("pageInfo", noticeService.queryForList(qo));
        return "systemNotice/release";
    }

    //已发送通知页面的删除通知的删除方法
    @RequestMapping("delete")
    public String delete(Long id) {
        noticeService.deleteByPrimaryKey(id);
        return "redirect:systemNotice/release";
    }
}